package com.qf.controller;

import com.qf.form.SmsInterfaceForm;
import com.qf.service.InterfaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/16 0016 9:55
 * description:映射下行接口的Controller
 */
@RequestMapping("sms")
@RestController
public class InterfaceController {

    Logger logger = LoggerFactory.getLogger(InterfaceController.class);

    @Autowired
    private InterfaceService interfaceService;


    //读取配置文件中,被Nginx代理时请求中的属性名
    @Value("${ip-header}")
    private List<String> ipHeaders;
//接收用户端请求,Nginx负载均衡后,将发送的短信数据
    @PostMapping("/smsInterface")
    public Map smsInterface(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){

        //1.获取用户请求中的IP地址
        String ip = getRealIpAddress(request);

        //2.讲IP地址放入短信对象中封装
        smsInterfaceForm.setIp(ip);

        //3.对各参数进行非空校验,
        Map resultMap = checkNotNull(smsInterfaceForm);
        if (resultMap != null){
            logger.info("[接口模块~接收用户请求] 参数非空校验失败",resultMap);
            return resultMap;
        }
        //4.调用service层,进一步校验并对短息对象中的数据初始化
        resultMap = interfaceService.checkInterFaceForm(smsInterfaceForm);

        //5.讲service的返回结果反馈给客户
        return resultMap;
    }

//校验短信对象中属性不为空
        private Map checkNotNull(SmsInterfaceForm smsInterfaceForm){
        Map<String,String> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(smsInterfaceForm.getClientID())){
            resultMap.put("code","101");
            resultMap.put("msg","认证错误:ClientId错误");
            return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getPwd())){
                resultMap.put("code","102");
                resultMap.put("msg","密码错误");
                return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getIp())){
                resultMap.put("code","103");
                resultMap.put("msg","Ip校验错误");
                return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getContent())){
                resultMap.put("code","104");
                resultMap.put("msg","消息长度错误,为空或超长(限定500字)");
                return resultMap;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getMobile())){
                resultMap.put("code","105");
                resultMap.put("msg","手机号错误");
                return resultMap;
        }

        if (StringUtils.isEmpty(smsInterfaceForm.getSrcID())){
                resultMap.put("code","106");
                resultMap.put("msg","下行编码(SrcID)错误");
                return resultMap;
        }

        return null;
     }



//获取用户端的IP地址,

    private String getRealIpAddress(HttpServletRequest request){

        //1.遍历Nginx中代理请求的属性名
        if(ipHeaders != null){
            for (String ipHeader : ipHeaders){
                //2.判断请求头不为空
                if (!StringUtils.isEmpty(ipHeader) && ipHeader.length()>0){
                    //3.获取Nginx代理的真实IP地址
                    String ip = request.getHeader(ipHeader);
                    //4.判断是否拿到Ip地址
                    if (!StringUtils.isEmpty(ip) && "unknown".equalsIgnoreCase(ip)){
                        return ip;
                    }
                }
            }
        }

        //5.如果循环接受依旧没有拿到IP,则认为没有被Nginx代理,之间正常获取IP
        return request.getRemoteAddr();
    }

}
